package com.zh.note.huawei.onehundred.异常的打卡记录;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author: zhanghuan
 * @date: 2023/8/4 21:56
 * @description: create it
 */
public class Main {
    public static void main(String[] args) {
        // Scanner sc = new Scanner(System.in);
        // int n = sc.nextInt();
        // String[] r = new String[n];
        // for (int i = 0; i < n; i++) {
        //     r[i] = sc.nextLine();
        // }
        String[] clockRecords = {"100000,10,1,ABCD,ABCD", "100001,50,10,ABCE,ABCE"};
        // String[] clockRecords = {"100000,10,1,ABCD,ABCD", "100000,50,10,ABCD,ABCD"};

        String res = solveMethod(clockRecords);
        System.out.println(res);


    }

    private static String solveMethod(String[] clockRecords) {
        List<String> list = Arrays.stream(clockRecords).collect(Collectors.toList());
        // ArrayList<String> list = IntStream.range(0, r.length).mapToObj(i -> r[i]).collect(Collectors.toCollection(ArrayList::new));
        Set<String> processed = new HashSet<>();
        StringBuilder builder = new StringBuilder();
        list.stream()
                .filter(record -> {
                    String[] s = record.split(",");
                    String actualDevice = s[3];
                    String registeredDevice = s[4];
                    int time = Integer.parseInt(s[1]);
                    int distance = Integer.parseInt(s[2]);
                    return !registeredDevice.equals(actualDevice)
                            || processed.contains(record)
                            || list.stream()
                            .anyMatch(tmp -> {
                                if (tmp.equals(record)) {
                                    return false;
                                }
                                String[] tmpSplit = tmp.split(",");
                                int time2 = Integer.parseInt(tmpSplit[1]);
                                int distance2 = Integer.parseInt(tmpSplit[2]);
                                return Math.abs(time - time2) < 60 && Math.abs(distance - distance2) > 5;
                            });
                }).forEach(item -> {
                    processed.add(item);
                    builder.append(item).append(";");
                });
        return builder.length() == 0 ? "null" : builder.substring(0, builder.length() - 1);


    }
}
